import wx
import wx.aui
import os.path

from wx.py.shell import Shell
from plugins.base.pane import Pane

class ShellPane(Pane):
  def __init__(self, parent):
    Pane.__init__(self, parent)

    sizer = wx.BoxSizer()
    self.SetSizer(sizer)
    
    self._shell = Shell(self)
    sizer.Add(self._shell, True, wx.EXPAND)

    self.setIndentationAndWhiteSpace()

  def getConfigFileName(self):
    return os.path.dirname(__file__) + os.path.sep + 'config.cfg'

  def runFile(self, filename):
    self._shell.runfile(filename)
##    self._shell.run(open(filename, 'r').read())

  def setIndentationAndWhiteSpace(self, indent_style=None):
    style = {'Size':'2',
             'ShowIndentGuide':'true',
             'BackspaceUnindents':'true',
             'TabIndents':'true',
             'TabWidth':'2',
             'UseTabs':'false',
             'ViewWhitespace':'false'}

    # set indent size
    if style.has_key('Size') and style['Size'] != '':
      try:
        self._shell.SetIndent(int(style['Size']))
      except TypeError, err:
        self._shell.SetIndent(4)
    else:
      self._shell.SetIndent(4)

    # set indentation guides
    if style.has_key('ShowIndentGuide') and style['ShowIndentGuide'] != '':
      if style['ShowIndentGuide'].lower() == 'true':
        self._shell.SetIndentationGuides(True)
      else:
        self._shell.SetIndentationGuides(False)
    else:
      self._shell.SetIndent(True)

    # set backspace unindents
    if style.has_key('BackspaceUnindents') and style['BackspaceUnindents'] != '':
      if style['BackspaceUnindents'].lower() == 'true':
        self._shell.SetBackSpaceUnIndents(True)
      else:
        self._shell.SetBackSpaceUnIndents(False)
    else:
      self._shell.SetBackSpaceUnIndents(True)

    # set tab indents
    if style.has_key('TabIndents') and style['TabIndents'] != '':
      if style['TabIndents'].lower() == 'true':
        self._shell.SetTabIndents(True)
      else:
        self._shell.SetTabIndents(False)
    else:
      self._shell.SetTabIndents(True)

    # set tab width
    if style.has_key('TabWidth') and style['TabWidth'] != '':
      try:
        self._shell.SetTabWidth(int(style['TabWidth']))
      except TypeError, err:
        self._shell.SetTabWidth(4)
    else:
      self._shell.SetTabWidth(4)

    # set use tabs
    if style.has_key('UseTabs') and style['UseTabs'] != '':
      if style['UseTabs'].lower() == 'true':
        self._shell.SetUseTabs(True)
      else:
        self._shell.SetUseTabs(False)
    else:
      self._shell.SetUseTabs(True)

    # set white space
    if style.has_key('ViewWhitespace') and style['ViewWhitespace'] != '':
      if style['ViewWhitespace'].lower() == 'true':
        self._shell.SetViewWhiteSpace(True)
      else:
        self._shell.SetViewWhiteSpace(False)
    else:
      self._shell.SetViewWhiteSpace(True)
